<?php

class AskControl extends CommonControl {

    public function index() {
        if (IS_POST) {
            $content = trim(adds_html($_POST['content']));
            $reward = (int) $_POST['reward'];
            $cid = (int) $_POST['cid'];
            $uid = (int) $_POST['uid'];
            $ask = M('user')->query('select ask from hd_user where uid=' . $uid);
            $ask = (int) $ask[0]['ask'] + 1;
            $asksql = "UPDATE hd_user set ask={$ask} where uid=" . $uid;
            if (M('category')->exec("INSERT INTO hd_ask(content,reward,cid,uid,time) values('{$content}','{$reward}','{$cid}','{$uid}'," . time() . ")") && M('user')->exec($asksql)) {
                $this->success('问题已提交！', __ROOT__);
            } else {
                $this->error('提交失败！');
            }
        }
        $this->assign_head();
        $data = M('category')->query('SELECT * FROM hd_category where pid=0');
        $this->assign('data', $data);
        $this->display();
    }

    public function ajax_category() {
        if (IS_AJAX) {
            $pid = (int) $_GET['pid'];
            $data = M('category')->query('SELECT * FROM hd_category where pid=' . $pid);
            echo json_encode($data);
        }
    }

}
